

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data
Imports Csla.Validation

<Serializable()> _
Public Class rw_playerChild
    Inherits Csla.BusinessBase(Of rw_playerChild)

#Region " Business Properties and Methods "

    ' declare members
    Private _playerId As Integer = 0
    Private _teamId As Integer = 0
    Private _playernameFirst As String = String.Empty
    Private _playernameLast As String = String.Empty

    ''' <summary>
    ''' Get the PlayerId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the PlayerId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    <System.ComponentModel.DataObjectField(True, True)> _
    Public ReadOnly Property PlayerId() As Integer
        Get
            CanReadProperty("PlayerId", True)
            Return _playerId
        End Get
    End Property

    ''' <summary>
    ''' Get or Set the TeamId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the TeamId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property TeamId() As Integer
        Get
            CanReadProperty("TeamId", True)
            Return _teamId
        End Get
        Set(ByVal value As Integer)
            CanWriteProperty("TeamId", True)
            If Not _teamId.Equals(value) Then
                _teamId = value
                PropertyHasChanged("TeamId")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the PlayernameFirst of the current object.
    ''' </summary>
    ''' <value>A String that represents the PlayernameFirst of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property PlayernameFirst() As String
        Get
            CanReadProperty("PlayernameFirst", True)
            Return _playernameFirst
        End Get
        Set(ByVal value As String)
            CanWriteProperty("PlayernameFirst", True)
            If value Is Nothing Then value = String.Empty
            If Not _playernameFirst.Equals(value) Then
                _playernameFirst = value
                PropertyHasChanged("PlayernameFirst")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the PlayernameLast of the current object.
    ''' </summary>
    ''' <value>A String that represents the PlayernameLast of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property PlayernameLast() As String
        Get
            CanReadProperty("PlayernameLast", True)
            Return _playernameLast
        End Get
        Set(ByVal value As String)
            CanWriteProperty("PlayernameLast", True)
            If value Is Nothing Then value = String.Empty
            If Not _playernameLast.Equals(value) Then
                _playernameLast = value
                PropertyHasChanged("PlayernameLast")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get the unique id for the current object.
    ''' </summary>
    ''' <returns>A value that is the unique id of the object.</returns>
    ''' <remarks></remarks>
    Protected Overrides Function GetIdValue() As Object
        Return _playerId
    End Function

#End Region ' Business Properties and Methods

#Region " Validation Rules "

    ''' <summary>
    ''' Method that allows the addition of custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddCustomRules()
        'todo: add custom/non-generated rules here...
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddSharedCustomRules()
        'todo: add custom/non-generated shared rules here...
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddInstanceBusinessRules()
        AddCustomRules()
        MyBase.AddBusinessRules()
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared common business rules.
    ''' </summary>
    ''' <remarks>These rules represent the database defaults that can be generated.</remarks>
    Private Sub AddSharedCommonRules()
        ' PlayernameFirst rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "PlayernameFirst")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("PlayernameFirst", 50))
        ' PlayernameLast rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "PlayernameLast")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("PlayernameLast", 50))
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddBusinessRules()
        AddSharedCommonRules()
        AddSharedCustomRules()
        MyBase.AddBusinessRules()
    End Sub

#End Region ' Validation Rules

#Region " Authorization Rules "

    Protected Overrides Sub AddAuthorizationRules()
        'TODO: Define authorization rules in rw_playerChild
        'AuthorizationRules.AllowRead("PlayerId", "rw_playerChildReadGroup")
        'AuthorizationRules.AllowRead("TeamId", "rw_playerChildReadGroup")
        'AuthorizationRules.AllowRead("PlayernameFirst", "rw_playerChildReadGroup")
        'AuthorizationRules.AllowRead("PlayernameLast", "rw_playerChildReadGroup")

        'AuthorizationRules.AllowWrite("TeamId", "rw_playerChildWriteGroup")
        'AuthorizationRules.AllowWrite("PlayernameFirst", "rw_playerChildWriteGroup")
        'AuthorizationRules.AllowWrite("PlayernameLast", "rw_playerChildWriteGroup")
    End Sub

#End Region ' Authorization Rules

#Region " Factory Methods "

    Public Shared Function NewPlayerChild() As rw_playerChild
        Return New rw_playerChild()
    End Function

    Friend Shared Function Getrw_playerChild(ByVal dr As SafeDataReader) As rw_playerChild
        Return New rw_playerChild(dr)
    End Function

    Private Sub New()

        ValidationRules.CheckRules()
        MarkAsChild()
    End Sub

    Private Sub New(ByVal dr As SafeDataReader)
        MarkAsChild()
        Fetch(dr)
    End Sub

#End Region ' Factory Methods

#Region " Data Access "

#Region " Data Access - Fetch "

    Private Sub Fetch(ByVal dr As SafeDataReader)
        FetchObject(dr)
        MarkOld()
        ValidationRules.CheckRules()

        'load child object(s)
        FetchChildren(dr)
    End Sub

    Private Sub FetchObject(ByVal dr As SafeDataReader)
        _playerId = dr.GetInt32("playerId")
        _teamId = dr.GetInt32("teamId")
        _playernameFirst = dr.GetString("playerName_first")
        _playernameLast = dr.GetString("playerName_last")
    End Sub

    Private Sub FetchChildren(ByVal dr As SafeDataReader)
    End Sub

#End Region ' Data Access - Fetch

#Region " Data Access - Insert "

    Friend Sub Insert(ByVal cn As SqlConnection, ByVal parent As rw_teamRoot)
        If Not IsDirty Then Return
        ExecuteInsert(cn, parent)
        MarkOld()

        UpdateChildren(cn)
    End Sub

    Private Sub ExecuteInsert(ByVal cn As SqlConnection, ByVal parent As rw_teamRoot)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_insPlayer"
            AddInsertParameters(cm, parent)
            cm.ExecuteNonQuery()

            _playerId = CType(cm.Parameters("@playerId").Value, Integer)
        End Using
    End Sub

    Private Sub AddInsertParameters(ByVal cm As SqlCommand, ByVal parent As rw_teamRoot)
        'todo: If parent use identity key, fix fk member with value from parent
        cm.Parameters.AddWithValue("@teamId", parent.TeamId)
        cm.Parameters.AddWithValue("@playerName_first", _playernameFirst)
        cm.Parameters.AddWithValue("@playerName_last", _playernameLast)
        cm.Parameters.AddWithValue("@playerId", _playerId)
        cm.Parameters("@playerId").Direction = ParameterDirection.Output
    End Sub

#End Region ' Data Access - Insert

#Region " Data Access - Update "

    Friend Sub Update(ByVal cn As SqlConnection, ByVal parent As rw_teamRoot)
        If Not IsDirty Then Return
        If MyBase.IsDirty Then
            ExecuteUpdate(cn, parent)
            MarkOld()
        End If

        UpdateChildren(cn)
    End Sub

    Private Sub ExecuteUpdate(ByVal cn As SqlConnection, ByVal parent As rw_teamRoot)

        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "esp_rw_playerchild_update"
            AddUpdateParameters(cm, parent)
            cm.ExecuteNonQuery()

        End Using
    End Sub

    Private Sub AddUpdateParameters(ByVal cm As SqlCommand, ByVal parent As rw_teamRoot)
        cm.Parameters.AddWithValue("@playerId", _playerId)
        cm.Parameters.AddWithValue("@teamId", _teamId)
        cm.Parameters.AddWithValue("@playerName_first", _playernameFirst)
        cm.Parameters.AddWithValue("@playerName_last", _playernameLast)
    End Sub

    Private Sub UpdateChildren(ByVal cn As SqlConnection)
    End Sub

#End Region ' Data Access - Update

#Region " Data Access - Delete "

    Friend Sub DeleteSelf(ByVal cn As SqlConnection)
        If Not IsDirty Then Return
        If IsNew Then Return
        ExecuteDelete(cn)
        MarkNew()
    End Sub

    Private Sub ExecuteDelete(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "esp_rw_playerchild_delete"
            cm.Parameters.AddWithValue("@playerId", _playerId)

            cm.ExecuteNonQuery()
        End Using
    End Sub

#End Region ' Data Access - Delete

#End Region ' Data Access


End Class
